<template>
  <v-layout>
    <template #header>
      <div style="display: flex;">
      <div style="margin-right: 40px;">时间选择：
      <el-date-picker
        v-model="timeData.timeRange"
        type="datetimerange"
        range-separator="-"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        value-format="YYYY-MM-DD HH:mm:ss"
        @change = "getPurchaseList"
      />
      </div>
      <div>学校选择：
      <el-select v-model="school" placeholder="请选择学校"  @change="igetSumOrderInfo($event)">
        <el-option
          v-for="item in schoolList"
          :key="item.cshopid"
          :label="item.cshopname"
          :value="item.cshopid"
        />
      </el-select>
      </div>
        <el-button class="Summary" type="primary" @click="singleOrder" 
        :disabled="tableData.length == 0?true:false">
          分单 
        </el-button>
      
    </div>
    </template>
    <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="scName" label="学校名称" width="180" />
        <el-table-column prop="cdesc" label="商品名称" width="180" />
        <el-table-column prop="cformat" label="规格" />
        <el-table-column prop="cunit" label="单位" width="180" />
        <el-table-column prop="price" label="价格" width="180" />
        <el-table-column prop="num" label="数量" />
        <el-table-column prop="ghName" label="供应商" width="180" />
    </el-table>
     <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 30, 50, 100]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    <!-- <v-table
      url="/querySummaryListBySchool"
      :query="tableQuery"
      AnyObject
      ref="tableRef"
    >
      <v-column auto prop="scName" label="学校名称"></v-column>
      <v-column auto prop="cdesc" label="商品名称"></v-column>
      <v-column auto prop="cformat" label="规格"></v-column>
      <v-column auto prop="cunit" label="单位"></v-column>
      <v-column auto prop="price" label="价格"></v-column>
      <v-column auto prop="num" label="数量"></v-column>
      <v-column auto prop="ghName" label="供应商"></v-column>
    </v-table> -->
  </v-layout>
</template>

<script setup lang="ts">
import { useTable } from "@/components/table/useTable";
import { markRaw, reactive, ref } from "vue";
import type { schoolData } from "@/api/user/model";
import { useUserStore } from "@/stores/user";
import { dayjs } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import { getSchoolList, singleOrderOption,tableList,querySummaryListBySchool } from "@/api/user";

const { tableRef, reload } = useTable();
//查询供应商信息
const { tznUser } = useUserStore();



const tableData = ref<any[]>([]); // 表格数据
  const currentDate = dayjs().format("YYYY-MM-DD HH:mm:ss");
  const time =dayjs().format("YYYY-MM-DD");
  const username = reactive({
  username: tznUser.name,
  shopId:'',
  beginTime : time+' '+'00:00:00',
  endTime :  time+' '+'23:59:59'
});
const tableQuery = reactive({
  goodsClass: "",
  shopId: "",
  // vendorid: "110",
  vendorid:tznUser.areaId,
  size: 10,
  current: 1,
  beginTime: time+' '+'00:00:00',
  endTime: time+' '+'23:59:59',
});
const timeData = reactive({
  timeRange:[time+' '+'00:00:00',time+' '+'23:59:59'], // 时间范围 [开始时间, 结束时间]
});

const getPurchaseList = () =>{
  
  tableQuery.beginTime = timeData.timeRange[0];
  tableQuery.endTime = timeData.timeRange[1];
  username.beginTime = timeData.timeRange[0];
  username.endTime = timeData.timeRange[1];
  querySummaryListBySchool(tableQuery).then((data) => {
    tableData.value=data.records
    total.value = data.total;
    
});
}
const schoolList = ref<schoolData[]>([]);
const school = ref<schoolData>({
  cshopid: "",
  cshopname: "",
});

//分页
const currentPage = ref(1)
const pageSize = ref(10)
const small = ref(false)
const disabled = ref(false)
const background = ref(false)
const total = ref(0); 
const handleSizeChange = (val: number) => {
  tableQuery.size=val
  querySummaryListBySchool(tableQuery).then((data) => {
    tableData.value=data.records
    total.value = data.total;
});
}
const handleCurrentChange = (val: number) => {
  tableQuery.current=val  
  querySummaryListBySchool(tableQuery).then((data) => {
    tableData.value=data.records
    total.value = data.total;
});
}
//获取学校信息
getSchoolList().then((data) => {
  schoolList.value = data;
  school.value = data[0];
  tableQuery.shopId = data[0].cshopid;
  username.shopId = data[0].cshopid;
  querySummaryListBySchool(tableQuery).then((data) => {
    tableData.value=data.records
    total.value = data.total;
});
  reload();
});

//根据学校选择
const igetSumOrderInfo = (event: any) => {
  // school.value = item;
  tableQuery.shopId = event;
  username.shopId =event;
  querySummaryListBySchool(tableQuery).then((data) => {
    tableData.value=data.records
    total.value = data.total;
    console.log(tableData);
    
});
  reload();
};
console.log(tableQuery.shopId,tableQuery.beginTime);


//分单操作
const singleOrder = () => {
  singleOrderOption(username).then((data) => {
    // console.log(tableData.value,'tableData');
    ElMessage.success("分单成功");
    querySummaryListBySchool(tableQuery).then((data) => {
    tableData.value=data.records
    total.value = data.total;
});
    reload();
  });
};
</script>
<style>
.Summary{
  margin-left: auto;
}
.el-pagination{
  float: right;
  margin-top: 20px;
}
</style>